package cn.edu.jsu.nrs.action;

import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.jsu.nrs.model.News;
import cn.edu.jsu.nrs.model.Reply;
import cn.edu.jsu.nrs.model.User;
import cn.edu.jsu.nrs.service.ReplyService;
import cn.edu.jsu.nrs.util.ConvertArray;

import com.opensymphony.xwork2.ActionSupport;

@Component(value = "replyAction")
@Scope(value = "prototype")
public class ReplyAction extends ActionSupport {
	private static final long serialVersionUID = 8244582382276140069L;
	private String ids;
	private int newsId;// 新闻id
	private int newsTypeId;// 新闻类别Id
	private int userId;// 用户id
	private int replyCount;// 跟帖数
	private int pageIndex;// 页的索引
	private int pageSize;// 页面大小
	private int result;// 结果
	private Reply reply;
	private List<Reply> replyList;// 回复列表
	private ReplyService replyService;
	private ConvertArray convertArray;
	private int rows;
	private int page;
	private int totalPage;// 总页数
	private int totalCount;// 总记录数
	private String sord;// 排序方式
	private String sidx;// 排序字段
	private String keyword;// 搜索条件
	private String content;// 内容

	/**
	 * 
	 * @return
	 */
	public String replyCount() {
		System.out.println("newsid=" + newsId);
		replyCount = replyService.getReplyCount(newsId);
		return "replyCount";
	}

	/**
	 * 获取新闻下面的部分跟帖
	 * 
	 * @return
	 */
	public String newsFirstReply() {
		replyList = replyService.getPagingReplyListByNewsId(newsId, 0, 10);// 获取部分跟帖
		return "replyList";
	}

	/**
	 * 分页查询所有跟贴列表
	 * 
	 * @return
	 */
	public String firstReplyList() {
		replyList = replyService.getFirstReplyList((page - 1) * rows, rows,
				keyword,newsTypeId, sord, sidx);
		totalCount = replyService.getFirstReplyCount(keyword,newsTypeId);
		System.out.println("replyList size=" + replyList.size());
		if (rows != 0 && replyList.size() != 0) {
			totalPage = (totalCount / rows + 1);
		}
		return "replyList";
	}

	/**
	 * 根据评论内容模糊查询评论列表
	 * 
	 * @return
	 */
	public String replyComments() {

		replyList = replyService.getReplyComments((page - 1) * rows, rows,
				content, sord, sidx);
		totalCount = replyService.getReplyCommentsCount(content, sord, sidx);
		if (rows != 0 && replyList.size() != 0) {
			totalPage = (totalCount / rows + 1);
		}
		return "replyList";
	}

	/**
	 * 新闻跟帖分页
	 * 
	 * @return
	 */
	public String replyPaging() {
		replyList = replyService.getPagingReplyListByNewsId(newsId, pageIndex
				* pageSize, pageSize);
		return "replyList";
	}

	/**
	 * 回复列表
	 * 
	 * @return
	 */
	public String replyList() {
		if (newsId != 0) {
			replyList = replyService.getReplyListByNewsId(newsId);
		}

		return "replyList";
	}

	/**
	 * 添加跟贴
	 * 
	 * @return
	 */
	public String addReply() {
		User user = new User();
		user.setUserId(userId);
		News news = new News();
		news.setNewsId(newsId);
		reply.setTbUser(user);
		reply.setTbNews(news);
		if (replyService.addReply(reply)) {
			result = 1;
			return "addReply";
		}
		result = 0;
		return "addReply";
	}

	/**
	 * 根据新闻ID删除跟贴
	 * 
	 * @return
	 */
	public String deleteReplyByNewsId() {
		if (replyService.deleteReplyById(newsId)) {
			result = 1;
		} else {
			result = 0;
		}
		return "deleteReply";
	}

	/**
	 * 批量删除跟贴/评论
	 * 
	 * @return
	 */
	public String deleteReplyByReplyIds() {
		System.out.println(ids);
		String[] deleteIds = convertArray.convertIdsToArray(ids);
		for (int i = 0; i < deleteIds.length; i++) {
			int id = Integer.parseInt(deleteIds[i].trim());
			Reply reply = replyService.getReply(id);
			if (reply != null) {
				System.out.println(reply.getContent());
				replyService.deleteReply(reply);
				result = 1;

			} else {
				result = 0;
				continue;
			}
		}
		return "deleteReply";
	}

	@JSON
	public int getResult() {
		return result;
	}

	@JSON
	public int getReplyCount() {
		return replyCount;
	}

	@JSON
	public List<Reply> getReplyList() {
		return replyList;
	}

	public void setNewsId(int newsId) {
		this.newsId = newsId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setNewsTypeId(int newsTypeId) {
		this.newsTypeId = newsTypeId;
	}

	@JSON
	public int getTotalPage() {
		return totalPage;
	}

	@Resource
	public void setConvertArray(ConvertArray convertArray) {
		this.convertArray = convertArray;
	}

	@Resource
	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}

}
